package og.example.views.group;
import og.example.views.MainFrame;
import og.example.controller.GroupController;
import og.example.dao.ClassDataProvider;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GroupFrame extends JFrame {
    private JTextField groupNameField;
    private JTextField classNameField;

    public GroupFrame() {
        setTitle("添加小组");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel(new GridLayout(3, 2));
        JLabel groupNameLabel = new JLabel("小组名称：");
        groupNameField = new JTextField(15);
        JLabel classNameLabel = new JLabel("所属班级：");
        classNameField = new JTextField(15);
        JButton addButton = new JButton("添加");
        JButton cancelButton = new JButton("返回");

        mainPanel.add(groupNameLabel);
        mainPanel.add(groupNameField);
        mainPanel.add(classNameLabel);
        mainPanel.add(classNameField);
        mainPanel.add(addButton);
        mainPanel.add(cancelButton);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String groupName = groupNameField.getText();
                String className = classNameField.getText();
                if (groupName.isEmpty() || className.isEmpty()) {
                    JOptionPane.showMessageDialog(GroupFrame.this, "小组名称和所属班级不能为空！");
                } else {
                    GroupController groupController = new GroupController();
                    boolean isAdded = groupController.groupAdd(groupName, className);
                    if (isAdded) {
                        JOptionPane.showMessageDialog(GroupFrame.this, "添加成功！");
                    } else {
                        JOptionPane.showMessageDialog(GroupFrame.this, "添加成功！");
                    }
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭当前窗口
                new MainFrame(); // 打开主界面
            }
        });
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String groupName = groupNameField.getText();
                String className = classNameField.getText();
                if (groupName.isEmpty() || className.isEmpty()) {
                    JOptionPane.showMessageDialog(GroupFrame.this, "小组名称和所属班级不能为空！");
                } else {
                    GroupController groupController = new GroupController();
                    boolean isAdded = groupController.groupAdd(groupName, className);
                    if (isAdded) {
                        JOptionPane.showMessageDialog(GroupFrame.this, "添加成功！");
                        // 更新班级的小组数量
                        ClassDataProvider.incrementGroupCountForClass(className);
                    } else {
                        JOptionPane.showMessageDialog(GroupFrame.this, "添加失败，小组名称可能已存在！");
                    }
                    dispose();
                    new MainFrame();
                }
            }
        });

        setContentPane(mainPanel);
        setVisible(true);
    }
}